import java.io.*;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

public class Storage {
	 private final AmazonS3 s3;
	 
	     public Storage(AWSCredentials credentials)
	     {
	        s3 = new AmazonS3Client(credentials);
	     }
	     
	     public Bucket createBucket(String bucketName) {
	         return s3.createBucket(bucketName);
	     }
	     
	     public void writeBucket(String bucketName) throws IOException{
	            //set key
	            String key = "object-name.txt";
	            
	            //set value
	            File file = File.createTempFile("temp", ".txt");
	            file.deleteOnExit();
	            Writer writer = new OutputStreamWriter(new FileOutputStream(file));
	            writer.write("What's the weather today?\r\nIt is a sunny day!");
	            writer.close();
	            
	            //put object - bucket, key, value(file)
	            s3.putObject(new PutObjectRequest(bucketName, key, file));
	            
	            //get object
	            S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
	            BufferedReader reader = new BufferedReader(
	            	    new InputStreamReader(object.getObjectContent()));
	            String data = null;
	            while ((data = reader.readLine()) != null) {
	                System.out.println(data);
	            }
	     }
	     public void removeBucket(String bucketName) {
	         s3.deleteBucket(bucketName);
	     }
	     
	     public void outputBucket(Bucket oldBucket) throws IOException{
         	//newStorage.createBucket(credentials.getAWSAccessKeyId().toLowerCase());
	    	 AmazonS3Client s3c = new AmazonS3Client();
             S3Object txtComplete = s3.getObject(oldBucket.getName(), "object-name.txt");
             System.out.println("S3Object, complete: " + txtComplete);

             // Read the data from the object's DataInputStream using a loop, and print it out.
             System.out.println("Testing:");
             BufferedReader reader = new BufferedReader(
                 new InputStreamReader(txtComplete.getObjectContent()));
             String data = null;
             while ((data = reader.readLine()) != null) {
                 System.out.println(data);
             }
	     }

	     public Bucket getBucket(String name) {
	         if(s3.doesBucketExist(name)) {
	             for(Bucket buck: s3.listBuckets()) {
	                 if(buck.getName().equals(name)) {
	                     System.out.println(buck.getName());
	                     return buck;
	                 }
	             }
	         }
	         System.err.println("Bucket with name: " + name + " is not present.");
	         return null;
	     }
}
